/*
   Copyright 2012 Ghafari Léo

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package openGLeo.graphics.shapes;

import static org.lwjgl.opengl.GL11.*;

import openGLeo.graphics.Shape;
import openGLeo.graphics.utils.Vector3D;

/**Classe permetant le dessin d'une pyramide a base carée.
 * @author Leo
 *
 */
public class Pyramid extends Shape{

	
	/**Constructeur initialisant une pyramide au centre de son repere parent
	 * avec une rotation nulle 
	 */
	public Pyramid()
	{
		this(Vector3D.NULL_VECTOR);
	}
	
	/**
	 * Constructeur initialisant une pyramide
	 * situé a une position donnée du repere parent, sans rotation
	 * @param location Emplacement de l'élément dans le repere parent
	 */
	public Pyramid(Vector3D location)
	{
		this(location,0,0,0,1,1);
	}
	
	
	/**
	 * Constructeur initialisant une pyramide a partir de sa position, de sa rotation initiale, de la largeur de sa base et de sa hauteur
	 * La coordonée en z du vecteur correspond au milieu en hauteur de la pyramide.
	 * @param location position de la pyramide dans le repere local
	 * @param yaw rotation Yaw de la pyramide (axe Z)
	 * @param pitch rotation pitch de la pyramide (axe Y)
	 * @param roll rotation roll de la pyramide (axe z)
	 * @param height hauteur de la pyramide
	 * @param baseWidth largeur de la base
	 */
	public Pyramid(Vector3D location, double yaw , double pitch, double roll , double height, double baseWidth)
	{
		super(location, yaw, pitch, roll);

		
		
		vertexs.add(new Vector3D( baseWidth / 2,  -baseWidth / 2 , -height/2));
		vertexs.add(new Vector3D(0, 0 , height/2));
		vertexs.add(new Vector3D( baseWidth / 2, baseWidth / 2 , -height/2));
		
		vertexs.add(new Vector3D( -baseWidth / 2,  baseWidth / 2 , -height/2));
		vertexs.add(new Vector3D(0, 0 , height/2));
		vertexs.add(new Vector3D( baseWidth / 2, baseWidth / 2 , -height/2));
		
		vertexs.add(new Vector3D( -baseWidth / 2,  -baseWidth / 2 , -height/2));
		vertexs.add(new Vector3D(0, 0 , height/2));
		vertexs.add(new Vector3D( baseWidth / 2, -baseWidth / 2 , -height/2));
		
		vertexs.add(new Vector3D( -baseWidth / 2,  -baseWidth / 2 , -height/2));
		vertexs.add(new Vector3D(0, 0 , height/2));
		vertexs.add(new Vector3D( -baseWidth / 2, baseWidth / 2 , -height/2));
		

		
		/*

		vertexs.add(new Vector3D(-a/2, dx , -height/2));
		vertexs.add(new Vector3D(0, -baseWidth , -height/2));
		vertexs.add(new Vector3D(a/2, dx , -height/2));*/
	
	}

	/**
	 * Dessine une pyramide apres avoir appliqué un changement de repere
	 */
	@Override
	public void draw() {
		if(!isVisible)
			return;

		glPushMatrix();
		{
			glTranslated(relativePosition.x, relativePosition.y, relativePosition.z);

			glRotated(relativeRotation.yaw, 0, 0, 1);
			glRotated(relativeRotation.pitch, 0, 1, 0);
			glRotated(relativeRotation.roll, 0, 0, 1);


			for(int i = 0; i < vertexs.size();i+=3)
			{
				if(isSqueleton)
					glBegin(GL_LINE_LOOP);
				else
					glBegin(GL_TRIANGLES);
				
				glColor3f(1,1, 1);
				glVertex3d(vertexs.get(i).x, vertexs.get(i).y, vertexs.get(i).z);
				glVertex3d(vertexs.get(i+1).x, vertexs.get(i+1).y, vertexs.get(i+1).z);
				glVertex3d(vertexs.get(i+2).x, vertexs.get(i+2).y, vertexs.get(i+2).z);

				glEnd();
			}
			
		

		}
		glPopMatrix();
		
		glBegin(GL_LINE_LOOP);
		glColor3f(1, 0, 0);
			glVertex3d(-2, 0,0);
			
			glVertex3d(0, 2, 0);
			
			glVertex3d(2, 0, 0);
			
			glVertex3d(-0, -2, 0);
			
		glEnd();

	}

}
